package com.example.administrator.energyida;

import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.energyida.fragment.DashboardFragment;
import com.example.administrator.energyida.fragment.HomeFragment;
import com.example.administrator.energyida.fragment.NotificationsFragment;
import com.example.administrator.energyida.fragment.PersonalFragment;
import com.xuexiang.xui.XUI;

public class MainActivity extends FragmentActivity {

    private RelativeLayout mRelativeLayout;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    Fragment fragment1 = new HomeFragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.RelativeLayout,fragment1).commit();
                    return true;
                case R.id.navigation_dashboard:
                    getSupportFragmentManager().beginTransaction().replace(R.id.RelativeLayout, new DashboardFragment()).commit();
                    return true;
                case R.id.navigation_personal:
                    getSupportFragmentManager().beginTransaction().replace(R.id.RelativeLayout, new PersonalFragment()).commit();
                    return true;
            }
            return false;
        }
    };

    private long lastBackKeyTime = -1;  // 上次按退出键

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRelativeLayout = findViewById(R.id.RelativeLayout);

        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        setHome();
    }

    /**
     * 初始加载HomeFragment
     */
    private void setHome() {
        getSupportFragmentManager().beginTransaction().replace(R.id.RelativeLayout,new HomeFragment()).commit();
    }

    /**
     * return desktop
     */
    @Override
    public void onBackPressed() {

        long timeNow  = System.currentTimeMillis();

        if(lastBackKeyTime == -1){
            Toast.makeText( this, "再按一次Back键", Toast.LENGTH_SHORT ).show();
            lastBackKeyTime = timeNow;
        }else {
            if(timeNow - lastBackKeyTime <= 2000){
                android.os.Process.killProcess(android.os.Process.myPid());
                super.onBackPressed();
            }else {
                Toast.makeText( this, "再按一次Back键", Toast.LENGTH_SHORT ).show();
                lastBackKeyTime = timeNow;
            }
        }
    }

}
